<?php
include './config/config.php';

$where = 1;

$content = $_GET['content'];

if (!empty($content)) {
  $where = "`name` LIKE '%$content%'";
}

// 获取当前的页码
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// 获取person表的数据总数
$CountSql = "SELECT COUNT(*) AS num FROM `pre_department` WHERE $where";
$count = query($CountSql);

// 每页显示多少条数据
$limit = 5;

// 每页显示多少个分页的页码数
$size = 5;

$ShowPage = page($page, $count['num'], $limit, $size);

// 偏移量 -> 索引值
// ? $start = ($page - 1); 有时候偏移量需要 乘上 $limit
$start = ($page - 1) * $limit;

$sql = "SELECT * FROM `pre_department` WHERE $where ORDER BY `id` ASC LIMIT $start,$limit ";

$dataList = queryAll($sql);

if ($_POST) {
  $where = "`id` = '{$_POST['id']}'";
  deleteById("department", $where);
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('common/meta.php'); ?>
</head>
<style>
  .pagination {
    text-align: center;
  }

  .btn-toolbar {
    display: flex;
    align-items: center;
  }

  #search_form {
    display: flex;
    align-items: center;
    margin: 0;
    margin-left: 20px;
  }
</style>

<body>
  <!--<![endif]-->

  <?php include_once('common/header.php'); ?>

  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">部门列表</h1>
    </div>
    <ul class="breadcrumb">
      <li>DepartmentList <span class="divider">/</span></li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='departmentAdd.php'"><i class="icon-plus"></i>添加部门</button>
          <!-- 查询 -->
          <form class="form-inline" id="search_form" method="get">
            <div class="form-group">

              <input type="search" class="form-control" id="inputPassword2" placeholder="请输入内容..." name="content" data-toggle="popover" title="提示" data-content="可输入部门名称" data-placement="top">

            </div>
            <button type="submit" class="btn btn-primary">搜索</button>
          </form>
        </div>
        <div class="well">
          <table class="table table-hover">
            <thead>
              <tr>
                <th>ID</th>
                <th>部门</th>
                <th style="width: 30px;">操作</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($dataList as $item) { ?>
                <tr>
                  <td>
                    <?php echo $item['id']; ?>
                  </td>
                  <td>
                    <?php echo $item['name']; ?>
                  </td>
                  <td>
                    <?php echo "<a href='departmentUpdata.php?depId={$item['id']}'><i class='icon-pencil'></i></a>" ?>

                    <?php echo "<a href='#myModal'  role='button' data-id='{$item['id']}' data-toggle='modal'  class='sureBtn'><i class='icon-remove'></i></a>" ?>
                  </td>
                </tr>
              <?php } ?>
            </tbody>
          </table>
        </div>
        <div class="pagination">
          <?php echo $ShowPage; ?>
        </div>

        <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">警告</h3>
          </div>
          <div class="modal-body">
            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>确定删除？</p>
          </div>
          <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
            <button class="btn btn-danger deleteBtn" data-dismiss="modal">删除</button>
          </div>
        </div>

        <?php include_once('common/footer.php'); ?>

      </div>
    </div>
  </div>


  <script src="./assets/lib/jquery-1.8.1.min.js"></script>
  <script>
    
    $(function() {
      $("[data-toggle='popover']").popover();
    });

    // todo 传递id
    $('.sureBtn').on('click', function() {
      $('.deleteBtn').attr("data-id", $(this).attr('data-id'))
    });

    // todo 删除
    $('.deleteBtn').on('click', function() {
      var depId = $(this).attr('data-id');
      $.ajax({
        type: "post",
        url: "departmentList.php",
        data: {
          id: depId
        },
        dataType: "json",
        success: function(response) {
          alert('删除成功！');
          location.reload();
        },
        error: function(error) { // 错误回调的函数
          console.log(error);
        }
      });
    })

    // todo 搜索
    // $("#search_form").on('submit', function (e) {
    //   e.preventDefault();
    //   let content = $("input[name='content']").val();

    //   $.ajax({
    //     type: "get",
    //     url: "departmentList.php",
    //     data: {
    //       content,
    //       actions:'search'
    //     },
    //     dataType: "json",
    //     success: function (res) {
    //       if (res.code === 1) {
    //         alert()
    //       }
    //     }
    //   });
    // });
  </script>
</body>

</html>
<?php include_once('common/script.php'); ?>